package com.itheima.ydd.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.AdvposPage;
import com.itheima.ydd.dto.YddAdvimgDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_advimg")
@Slf4j
public class YddAdvimgController {

    @Autowired
    private IYddAdvimgService yddAdvimgService;
    @Autowired
    private IYddAdvposService yddAdvposService;

    /**
     * 广告图列表
     * @param advimgdesc
     * @param advimgpos
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData page(String advimgdesc, String advimgpos, Integer page, Integer limit){
//        System.out.println(advimgdesc);
//        System.out.println(advimgpos);
//        System.out.println(page);
//        System.out.println(limit);
        LambdaQueryWrapper<YddAdvimg> lqw = new LambdaQueryWrapper<>();
        lqw.like(!StringUtils.isEmpty(advimgdesc),YddAdvimg::getAdvimgdesc,advimgdesc).eq(!org.springframework.util.StringUtils.isEmpty(advimgpos),YddAdvimg::getAdvimgpos,advimgpos);
        Page<YddAdvimg> pageInfo = new Page<>(page,limit);
        yddAdvimgService.page(pageInfo,lqw);
        Page<YddAdvimgDto> dtoPage=new Page<>();
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<YddAdvimg> records1 = pageInfo.getRecords();
        List<YddAdvimgDto> list=new ArrayList<>();

        for (YddAdvimg records : records1) {
            YddAdvimgDto yddAdvimgDto = new YddAdvimgDto();
            yddAdvimgDto.setId(records.getId());
            yddAdvimgDto.setAdvimgdesc(records.getAdvimgdesc());
            yddAdvimgDto.setAdvimglink(records.getAdvimglink());
            yddAdvimgDto.setAdvimgpos(records.getAdvimgpos());
            yddAdvimgDto.setAdvimgsrc(records.getAdvimgsrc());
            yddAdvimgDto.setDefaultData(records.getDefaultData());
            YddAdvpos advpos = yddAdvposService.getById(records.getAdvimgpos());
            log.info("查询的数据是:{}",advpos.toString());
            String advposname = advpos.getAdvposname();
            yddAdvimgDto.setAdvposname(advposname);
            list.add(yddAdvimgDto);
        }
        dtoPage.setRecords(list);
        AdvposPage advposPage = new AdvposPage();
        advposPage.setCount((int) pageInfo.getTotal());
        advposPage.setCurrentPage((int) pageInfo.getCurrent());
        advposPage.setPageSize((int) pageInfo.getSize());
        advposPage.setTotalPages((int) pageInfo.getPages());
        advposPage.setData(dtoPage.getRecords());
       return ResultData.ok("成功",advposPage);
    }

    /**
     * 广告图详情
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData getById(Integer id){
        YddAdvimg yddAdvimg = yddAdvimgService.getById(id);
        if (yddAdvimg!=null){
            return ResultData.ok("cheng",yddAdvimg);
        }
        return ResultData.error("失败");
    }

    /**
     * 修改广告图
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody YddAdvimg yddAdvimg){
        log.info("yddAdvimg:{}",yddAdvimg.toString());
        yddAdvimgService.updateById(yddAdvimg);
        return R.success("Code",200);
    }

    /**
     * 添加
     * @param yddAdvimg
     * @return
     */
    @GetMapping("/add")
    public R add(@RequestBody YddAdvimg yddAdvimg){
        yddAdvimgService.save(yddAdvimg);
        return R.success("Code",200);
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R deleteById(Integer id){
        yddAdvimgService.removeById(id);
        return R.success("Code",200);
    }
}
